/**
 * Created by yangxianming on 2018/6/11.
 * data 数组定义
 * 用例名 最低消费金额 最高折扣金额	 订单金额 不参与优惠金额 折扣值	 福利金余额 员工尊享福利 福利金抵扣 实际支付金额 商家实收
 */

var data = [
    ["小额","2","1","2","","9.9","","","","",""],
    ["无限制","","","12","2","8","5","","","",""],
    ["订单,不参与金额有小数","","","12.22","2.34","8","","","","",""],
    ["订单,不参与金额有小数","","","18.18","1.16","8","","","","",""],
    ["最低消费金额-生效","20","","19","9","8","","","","",""],
    ["最低消费金额-临界值","20","","20","9","8","","","","",""],
    ["最低消费金额-未生效","20","","21","9","8","","","","",""],
    ["最高折扣金额-未生效","","10","21","9","8","","","","",""],
    ["最高折扣金额-生效","","10","101","1","8","","","","",""],
    ["双限制","20","10","101","1","8","","","","",""],
    ["双限制","20","10","20","1","8","","","1","",""],
    ["订单,不参与金额有小数","20","10","16.16","1.18","8","","","","",""],
    ["订单,不参与金额有小数","20","10","98.68","68.98","8","","","","",""],
    ["订单,不参与金额有小数","20","10","98.68","8.68","8","","","","",""],
    ["福利金抵扣","20","10","98.68","8.68","8","","","","",""],
    ["不参与金额为空为零","20","10","98.68","","8","","","","",""],
    ["不参与金额为空为零","20","10","98.68","0","8","","","","",""],
    ["福利金抵扣超过100元","20","10","200","0","8","500","","","",""],
    ["福利金抵扣不超过100元","20","10","20","0","8","500","","","",""],
    ["福利金抵扣不超过100元","20","10","100","0","8","500","","","",""],
    ["福利金抵扣不超过100元","20","10","100","0","8","5","","","",""],
    ["订单,福利金带有小数","20","10","98.18","0","8","5","","","",""],
    ["订单,福利金带有小数","20","10","98.18","0","8","13.45","","","",""],
    ["订单,福利金带有小数","20","10","8.5","0","8","53.45","","","",""],
    ["订单金额为空为零","20","10","0","18.4","8","","","","",""],
    ["订单金额为空为零","20","10","","18.4","8","","","","",""],
    ["订单金额为空为零","","","0","18.4","8","","","","",""],
    ["订单金额为空为零","","","","18.4","8","","","","",""],

    ["折扣值设置零或空","","","120","20","","","","","",""],
    ["折扣值设置零或空","","","120","20","0","","","","",""],
    ["折扣值最小或最大","","","120","20","0.1","","","","",""],
    ["折扣值最小或最大","","","120","20","9.9","","","","",""]
];
function initializeData(){
    cal();
    var tableInfo = document.getElementById("cal").getElementsByTagName("tbody")[0];
    for(var m = 0; m < data.length; m++){
        var tr = document.createElement("tr");
        if((m%2)==1){
            tr.setAttribute("style", "background-color: #ECEAFF");
            tr.setAttribute("originalColor", "background-color: #ECEAFF");
        }
        //#ffeaea
        tr.onmouseover = function(){
            if (this.getAttribute("id") != "highLight"){
                this.setAttribute("style", "background-color: #ffeaea");
            }
        };
        tr.onmouseout = function(){
            if (this.getAttribute("id") != "highLight"){
                this.setAttribute("style", this.getAttribute("originalColor"));
            }
        };
        tr.onclick = function(){
            var highLight = document.getElementById("highLight");
            if(highLight != null){
                highLight.setAttribute("style", highLight.getAttribute("originalColor"));
                highLight.removeAttribute("id");
            }
            this.setAttribute("style", "background-color: #ec6262");
            this.setAttribute("id", "highLight");
        };
        for(var n = 0; n < data[m].length; n++){
            var td = document.createElement("td");
            td.innerText = data[m][n];
            tr.appendChild(td);
        }
        tableInfo.appendChild(tr);
    }
    //添加操作 colspan
    var tr1 = document.createElement("tr");
    tableInfo.appendChild(tr1);
    var td1 = document.createElement("td");
    td1.setAttribute("colspan", 11);
    td1.setAttribute("style", "text-align:center;cursor:pointer;");
    //td1.setAttribute("onclick", "addcol()");
    td1.onclick = function(){addcol()};
    td1.innerText = "+";
    tr1.appendChild(td1);
}

function addcol(){
    var tableInfo = document.getElementById("cal").getElementsByTagName("tbody")[0];
    var tableLength = document.getElementById("cal").getElementsByTagName("tbody")[0].getElementsByTagName("tr").length-1;
    var row = tableInfo.insertRow(tableLength);
    row.onclick = function(){calone(this)};

    for(var i = 0; i < 11; i++){
        if(i < 7){
            var myInput = document.createElement("input");
            myInput.setAttribute("class", "discount");
            var cell = row.insertCell(i);
            cell.appendChild(myInput);
            continue;
        }
        var cell1 = row.insertCell(i);
        cell1.innerText = "";
    }
}

function calone(tr){
    var min = Number(tr.getElementsByTagName("td")[1].getElementsByTagName("input")[0].value);
    var max = Number(tr.getElementsByTagName("td")[2].getElementsByTagName("input")[0].value);
    var order = Number(tr.getElementsByTagName("td")[3].getElementsByTagName("input")[0].value);
    var noOrder = Number(tr.getElementsByTagName("td")[4].getElementsByTagName("input")[0].value);
    var orderDiscount = Number(tr.getElementsByTagName("td")[5].getElementsByTagName("input")[0].value);
    var welfareCount = Number(tr.getElementsByTagName("td")[6].getElementsByTagName("input")[0].value);

    var emp = 0;
    var fldk = 0;
    var realPay = 0;
    var businessPay = 0;

    //算出尊享福利
    var discount = (1 - Number(orderDiscount) * 0.1).toFixed(2);
    emp = ((Number(order) - Number(noOrder)) * discount).toFixed(2);
    if (Number(emp) > max && (max != "" || max != 0)){
        emp = max;
    }
    if(orderDiscount == 0){
        emp = 0;
    }
    if(order == 0){
        emp = 0;
    }
    if(order < min){
        emp = 0;
    }

    //福利金抵扣值
    var diff = order - emp;
    if(diff >= welfareCount){
        fldk = welfareCount;
    }else{
        fldk = diff;
    }
    if(fldk > 100){
        fldk = 100;
    }
    var afterPoint = "0";
    var fldkString = fldk.toString();
    if(fldkString.indexOf(".") != -1){
        afterPoint = "0" + fldkString.substring(fldkString.indexOf("."), fldk.length);
        fldk = fldk - Number(afterPoint);
    }


    //算出实际支付金额
    if (Number(order) < Number(min)){
        realPay = order - fldk;
    }else{
        //福利金小于1元不参与抵扣
        if(emp < 1){
            realPay = order - fldk;
        }else{
            realPay = order - emp - fldk;
        }
    }
    if (realPay < 0){
        realPay = 0;
    }
    realPay = (Number(realPay.toFixed(2)) + Number(afterPoint)).toFixed(2);

    //算出商家实收
    businessPay = (Number(fldk) + Number(realPay)).toFixed(2);

    //插入数据
    tr.getElementsByTagName("td")[7].innerText = emp;
    tr.getElementsByTagName("td")[8].innerText = fldk;
    tr.getElementsByTagName("td")[9].innerText = realPay;
    tr.getElementsByTagName("td")[10].innerText = businessPay;
}

function cal(){
    for(var m = 0; m < data.length; m++){
        var min = Number(data[m][1]);   //最低消费金额
        var max = Number(data[m][2]);   //最高折扣金额
        var order = Number(data[m][3]);   //订单金额
        var noOrder = Number(data[m][4]);   //不参与优惠金额
        var orderDiscount = Number(data[m][5]);   //折扣值
        var welfareCount = Number(data[m][6]);   //福利金余额
        var emp = Number(data[m][7]);   //员工尊享福利
        var fldk = 0;   //福利金抵扣
        var realPay = 0;   //实际支付金额
        var businessPay = 0;   //商家实收


        //算出尊享福利
        var discount = (1 - orderDiscount * 0.1).toFixed(2);
        emp = ((order - noOrder) * discount).toFixed(2);
        if (emp > max && (max != "" || max != 0)){
            emp = max;
        }
        if(orderDiscount == 0){
            emp = 0;
        }
        if(order == 0){
            emp = 0;
        }
        if(order < min){
            emp = 0;
        }

        //福利金抵扣值
        var diff = order - emp;
        if(diff >= welfareCount){
            fldk = welfareCount;
        }else{
            fldk = diff;
        }
        if(fldk > 100){
            fldk = 100;
        }
        var afterPoint = "0";
        var fldkString = fldk.toString();
        if(fldkString.indexOf(".") != -1){
            afterPoint = "0" + fldkString.substring(fldkString.indexOf("."), fldk.length);
            fldk = fldk - Number(afterPoint);
        }


        //算出实际支付金额
        if (order < min){
            realPay = order - fldk;
        }else{
            //福利金小于1元不参与抵扣
            if(emp < 1){
                realPay = order - fldk;
            }else{
                realPay = order - emp - fldk;
            }
        }
        if (realPay < 0){
            realPay = 0;
        }
        realPay = (Number(realPay.toFixed(2)) + Number(afterPoint)).toFixed(2);

        //算出商家实收
        businessPay = (Number(fldk) + Number(realPay)).toFixed(2);

        //计算好插入数据
        data[m][7] = emp;   //员工尊享福利
        data[m][8] = fldk;   //福利金抵扣
        data[m][9] = realPay;   //实际支付金额
        data[m][10] = businessPay;   //商家实收
    }
}